package com.mvc.base.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.mvc.base.model.Picture;
import com.mvc.framework.model.CommonAuditableEntity;
import com.mvc.framework.service.BaseService;

/**
 * 
 * @author snowpbx
 * @version V1.0 Create Time: 2012-11-1
 */
@Service
public class PictureManager extends BaseService<Picture, Integer> {

	public void deleteByFileId(Long fileId) {
		deleteByWhere("fileId=" + fileId);
	}

	public List<Picture> getPicturesByItemIdAndType(Integer itemId, Integer type) {
		return searchBySql("select A from " + Picture.class.getName() + " A where A.itemId =? AND type=? AND recStatus=?", new Object[] {
		        itemId, type, CommonAuditableEntity.REC_STATUS_VALID });
	}

	public void updateOrderId(Integer oldOrderId, Integer newOrderId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + Picture.class.getName() + " set itemId = " + newOrderId + " where itemId = "
		        + oldOrderId + " and type=" + Picture.PICTURE_OF_ORDER);
		query.executeUpdate();
	}

	public void updatePurchaseId(Integer oldOrderId, Integer newOrderId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + Picture.class.getName() + " set itemId = " + newOrderId + " where itemId = "
		        + oldOrderId + " and type=" + Picture.PICTURE_OF_PURCHASE);
		query.executeUpdate();
	}

	public void updateQuotationId(Integer oldOrderId, Integer newOrderId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + Picture.class.getName() + " set itemId = " + newOrderId + " where itemId = "
		        + oldOrderId + " and type=" + Picture.PICTURE_OF_QUOTATION);
		query.executeUpdate();
	}

	public void updateFileRemark(Long fileId, String remark) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + Picture.class.getName() + " set remark = ? where fileId = ?");
		query.setParameter(0, remark);
		query.setParameter(1, fileId);
		query.executeUpdate();
	}
}
